articles

Home / DeveloperSection / Articles / Nested Classes in Java: Classifications of Nested Classes

Nested Classes in Java: Classifications of Nested Classes

Jayden Bell1774 19-May-2016

The nested classes are classified as follows:

·         static

·         non-static (or inner)

·         local

·         anonymous

Static Nested Classes:        

To create a static nested class, we use the static keyword in front of the class declaration, the same way we declare a static field or a method within the enclosing class. Just like a static method cannot refer to the non-static members of the enclosing class, a static inner class cannot refer to the non-static members of its enclosing class. To refer to the non-static members, it has to use an object reference to the enclosing class. To access a static nested class, we need to use its fully qualified name, using the syntax OuterClassName.StaticNestedClassName. To create an object of a static nested class, use the following syntax:

OuterClassName.StaticNestedClassName ClassObjectName =                                                                       new OuterClassName.StaticNestedClassName();

From this syntax, we can see that a static nested class is just like any other top-level class. It is simply nested in another top-level class for packaging convenience.

Non-static Nested class or Inner class:

As mentioned earlier, the non-static nested classes are also called inner classes. An inner class is associated with an instance of its enclosing class.

·    It has direct access to the outer class’s fields and methods.

·    An inner class cannot define any static members because it is always associated with an instance.

·    An instance of an inner class can exist only within an instance of its outer class.

·    We may create multiple instances of the same inner class within a single instance of the enclosing class.

To create an instance of the inner class, we must have an object of the outer class. The following code snippet illustrates this:

InnerOddsIterator iterator = this.new InnerOddsIterator();

while (iterator.hasNext()) {

int returnValue = iterator.getNext();

 

The InnerOddsIterator class is an inner class. To create an instance of this class, we use the call this.new, where this refers to the instance of the current class. The new keyword instantiates the class specified on its right side. The reference to the instance of the inner class is held in the iterator variable. We will use this variable to access the members of the inner class. Both hasNext and getNext are the methods of this inner class.

Local and Anonymous classes:

The last two types of nested classes—that is, local and anonymous—fall under the category of inner classes.

·     An inner class defined within the body of a method is called a local inner or simply a local class.

·    The scope of a local class is restricted to the method’s scope.

·    An anonymous inner class is an inner class declared within the body of a method without a name given to it. We will use anonymous classes while studying the building of a GUI


Updated 16-Dec-2017

Leave Comment

Comments

Liked By